package com.Xht.www.servlet;

//后端响应商家注册请求

import com.Xht.www.dao.MerchantInfoDAO;
import com.Xht.www.po.MerchantInfoPO;
import com.Xht.www.util.ValidationUtil;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/merchant/register")
public class MerchantRegisterServlet extends HttpServlet {
    private MerchantInfoDAO merchantInfoDAO = new MerchantInfoDAO();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应格式为 JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        // 获取请求参数
        String merchantName = request.getParameter("merchantName");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");
        String address = request.getParameter("address");
        String establishmentYearStr = request.getParameter("establishmentYear");
        String contactInfo = request.getParameter("contactInfo");
        String keywords = request.getParameter("keywords");

        int establishmentYear;
        try {
            establishmentYear = Integer.parseInt(establishmentYearStr);
        } catch (NumberFormatException e) {
            out.write("{\"success\": false, \"message\": \"成立年份必须为有效数字\"}");
            out.close();
            return;
        }

        // 构建参数 Map 用于工具类校验
        Map<String, String> params = new HashMap<>();
        params.put("merchantName", merchantName);
        params.put("password", password);
        params.put("confirmPassword", confirmPassword);
        params.put("address", address);
        params.put("establishmentYear", establishmentYearStr);
        params.put("contactInfo", contactInfo);
        params.put("keywords", keywords);

        // 调用工具类进行输入校验
        ValidationUtil.ValidationResult result = ValidationUtil.validateInput("merchant-register", params);

        if (!result.isSuccess()) {
            out.write("{\"success\": false, \"message\": \"" + result.getMessage() + "\"}");
            out.close();
            return;
        }

        // 参数合法，设置默认值并创建 PO 对象
        String businessStatus = "正常";
        String applicationStatus = "申请中";
        int salesVolume = 0;
        int favoritesCount = 0;
        BigDecimal turnover = BigDecimal.ZERO;

        MerchantInfoPO merchant = new MerchantInfoPO();
        merchant.setMerchantName(merchantName);
        merchant.setPassword(password);
        merchant.setAddress(address);
        merchant.setEstablishmentYear(establishmentYear);
        merchant.setContactInfo(contactInfo);
        merchant.setKeywords(keywords);
        merchant.setBusinessStatus(businessStatus);
        merchant.setApplicationStatus(applicationStatus);
        merchant.setSalesVolume(salesVolume);
        merchant.setFavoritesCount(favoritesCount);
        merchant.setTurnover(turnover);

        // 插入数据库
        if (merchantInfoDAO.insertMerchantInfo(merchant)) {
            out.write("{\"success\": true, \"message\": \"商家注册成功\"}");
        } else {
            out.write("{\"success\": false, \"message\": \"商家注册失败，可能名称已存在\"}");
        }
        out.close();
    }
}
